package com.coolpad.Utils;


public final class ByteUtils {
    public ByteUtils() {
    }

    public static short readShort(byte[] bytes, int offset) {
        return (short)(bytes[offset] << 8 | bytes[offset + 1] & 255);
    }

    public static int read2ByteInt(byte[] bytes, int offset) {
        int i = 0;
        i = i | bytes[offset] & 255;
        i <<= 8;
        i |= bytes[offset + 1] & 255;
        return i;
    }

    public static int readInt(byte[] bytes, int offset) {
        return (bytes[offset + 0] & 255) << 24 | (bytes[offset + 1] & 255) << 16 | (bytes[offset + 2] & 255) << 8 | bytes[offset + 3] & 255;
    }

    public static long readUnsignedInt(byte[] bytes, int offset) {
        return ((long)bytes[offset + 0] & 255L) << 24 | ((long)bytes[offset + 1] & 255L) << 16 | ((long)bytes[offset + 2] & 255L) << 8 | (long)bytes[offset + 3] & 255L;
    }

    public static long readLong(byte[] bytes, int offset) {
        return (long)(bytes[offset + 0] & 255) << 56 | (long)(bytes[offset + 1] & 255) << 48 | (long)(bytes[offset + 2] & 255) << 40 | (long)(bytes[offset + 3] & 255) << 32 | (long)(bytes[offset + 4] & 255) << 24 | (long)(bytes[offset + 5] & 255) << 16 | (long)(bytes[offset + 6] & 255) << 8 | (long)bytes[offset + 7] & 255L;
    }

    public static long readBytes(byte[] bytes, int offset, int numBytes) {
        int shift = 0;
        long value = 0L;

        for(int i = offset + numBytes - 1; i >= offset; --i) {
            value |= ((long)bytes[i] & 255L) << shift;
            shift += 8;
        }

        return value;
    }

    public static void writeShort(byte[] bytes, short value, int offset) {
        bytes[offset] = (byte)(255 & value >> 8);
        bytes[offset + 1] = (byte)(255 & value);
    }

    public static void writeInt(byte[] bytes, int value, int offset) {
        bytes[offset] = (byte)(255 & value >> 24);
        bytes[offset + 1] = (byte)(255 & value >> 16);
        bytes[offset + 2] = (byte)(255 & value >> 8);
        bytes[offset + 3] = (byte)(255 & value);
    }

    public static void writeLong(byte[] bytes, long value, int offset) {
        bytes[offset] = (byte)((int)(255L & value >> 56));
        bytes[offset + 1] = (byte)((int)(255L & value >> 48));
        bytes[offset + 2] = (byte)((int)(255L & value >> 40));
        bytes[offset + 3] = (byte)((int)(255L & value >> 32));
        bytes[offset + 4] = (byte)((int)(255L & value >> 24));
        bytes[offset + 5] = (byte)((int)(255L & value >> 16));
        bytes[offset + 6] = (byte)((int)(255L & value >> 8));
        bytes[offset + 7] = (byte)((int)(255L & value));
    }

    public static void writeBytes(byte[] bytes, long value, int offset, int numBytes) {
        int shift = 0;

        for(int i = offset + numBytes - 1; i >= offset; --i) {
            bytes[i] = (byte)((int)(255L & value >> shift));
            shift += 8;
        }

    }

    public static byte[] intToByteArray(int number) {
        int temp = number;
        byte[] b = new byte[4];

        for(int i = b.length - 1; i > -1; --i) {
            b[i] = Integer.valueOf(temp & 255).byteValue();
            temp >>= 8;
        }

        return b;
    }
}

